<?php
/**
 * Created by PhpStorm.
 * User: Dave
 * Date: 2018/4/11
 * Time: 15:10
 */

namespace Biz\model\activity;

use Biz\tools\BizUrl;
use Doraemon\tools\ImgUrl;
use Biz\service\Goods;

class RecommendGoods extends \Doraemon\pockets\datebase\BaseDbModel
{
    const _CONFIG_FILE_ = 'db_shop';

    const _CONFIG_SELECT_ = 'db_activity';

    const _DATABASE_ = 'shop_activity';

    const _TABLE_NAME = 'recommend_goods';

    const _SUB_TABLE = false;

    // 状态为展示
    const STATUS_NORMAL = 1;
    // 状态为隐藏
    const STATUS_HIDE = 2;
    // 状态为被删除
    const STATUS_DELETE = 9;

    public static function getValidList($offset = 0, $size = 10)
    {
        $list = self::select("*", [
            'LIMIT' => [$offset, $size],
            'status' => self::STATUS_NORMAL,
            'ORDER' => [
                'sort',
                'id' => 'DESC'
            ]
        ]);
        return $list;
    }
    public static function getValidCount()
    {
        $list = self::count("*", [
            'status' => self::STATUS_NORMAL
        ]);
        return $list;
    }

    public static function getList($where)
    {
        if (!isset($where['ORDER'])) {
            $where['ORDER'] = [
                'status',
                'sort',
                'id' => 'DESC'
            ];
        }
        $list = self::select("*", $where);
        if (isset($where['LIMIT'])) {
            unset($where['LIMIT']);
        }
        if (isset($where['ORDER'])) {
            unset($where['ORDER']);
        }
        $total = self::count('*',$where);
        if ($list) {
            $goodsIds = array_column($list, 'goods_id');
            $goodsInfo = Goods::Goods($goodsIds);
            foreach ($list as &$info) {
                $info = self::formatInfo($info);
                $info['goods_info'] = isset($goodsInfo[$info['goods_id']]) ? $goodsInfo[$info['goods_id']] : [];
            }
        }
        return compact('list', 'total');
    }

    public static function getPageInfoById($pageId)
    {
        $pageinfo = self::get("*", ['id' => $pageId]);
        if ($pageinfo) {
            return self::formatPageInfo($pageinfo);
        } else {
            return [];
        }
    }

    /**
     * @desc 批量添加记录
     * @param $goodsIds
     * @return bool
     */
    public static function addGoods($goodsIds)
    {
        foreach ($goodsIds as $goodsId) {
            $data = [
                'goods_id' => $goodsId,
                'status' => self::STATUS_HIDE,
                'addtime' => date('Y-m-d H:i:s')
            ];
            self::insert($data);
        }
        return true;
    }

    private static function formatInfo($info)
    {
        $info['source_pic_id'] = $info['pic_id'];
        $info['pic_id'] = $info['pic_id'] ? ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $info['pic_id'], ImgUrl::IMAGE_SIZE_OP_ORI) : '';
        return $info;
    }

    public static function saveSortInfo($sortInfo)
    {
        foreach ($sortInfo as $id => $sort) {
            self::update(['sort' => $sort], ['id' => $id]);
        }
    }

    /**
     * @desc 修改状态
     * @param $id
     * @param $status
     * @return bool
     */
    public static function changeStatusById($id, $status)
    {
        if (self::update(['status' => $status], ["id" => $id])->rowcount()) {
            return $id;
        }
    }

    /**
     * @desc 修改信息
     * @param $id
     * @param $info
     * @return bool
     */
    public static function changeInfoById($id, $info)
    {
        if (self::update($info, ["id" => $id])->rowcount()) {
            return $id;
        }
    }

}